<template>
  <div class="marketing-coupon-container">
    <div class='header'>
      <el-button type='primary' @click="dialogVisible = true">添加商品</el-button>
      <span>商品价格，数量等信息，以商品设置为准；不能选择秒杀，拼团，砍价活动商品；需设置不能加入购物车；限购按需设置，以免损失。</span>
    </div>
    <!--表格-->
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="活动id"
        width="180">
      </el-table-column>
      <el-table-column
        prop="goods_id"
        label="商品id"
        width="180">
      </el-table-column>
      <el-table-column
        label="主图">
        <template slot-scope="scope">
          <div class='goodsimg'>
            <img class='goodsimg-item'  :key='item' :src='scope.row.pic_urls'/>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="price"
        label="价格">
      </el-table-column>
      <el-table-column
        prop="stocks"
        label="库存">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <!--dialog-->
    <el-dialog title="选择商品" width="500px" :visible.sync="dialogVisible">
        <div class='dialog-list'>
          <el-radio-group v-model="checked">
            <el-radio  v-for='(item) in goodsList' :key='item.id' :label="item.id">
              <img class='item-picurl' style='width: 40px; height: 40px;' :src='item.pic_urls.split(",")[0]'>
                <span class='item-yuan'>{{item.name}}</span>
            </el-radio>
          </el-radio-group>
        </div>
        <div class='dialog-button'>
          <el-button type='primary' @click="post">确定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
// import mixin from './components/mixin'
import { merchantCashback, putMerchantCashback, delMerchantCashback } from '@/api/marketing'
import { getGoodsList} from '@/api/goods'
export default {
  name: 'coupon',
  // mixins: [mixin],
  components: {
  },  
  data() {
    return {
      page: 1,
      count: 1,
      type: '10',
      dialogVisible: false,
      list: [],
      goodsList: [],
      checked: 0,
      tableData: [
        {id: 1, goodsid: 2, picurls:['https://imgs.juanpao.com/merchant%2Fshop%2Fgoods_picture%2F13%2FccvWPn%2F15723284125db7d3dcd0d98.jpeg','https://imgs.juanpao.com/merchant%2Fshop%2Fgoods_picture%2F13%2FccvWPn%2F15723284125db7d3dcd0d98.jpeg','https://imgs.juanpao.com/merchant%2Fshop%2Fgoods_picture%2F13%2FccvWPn%2F15723284125db7d3dcd0d98.jpeg'], title: '嘻嘻嘻哈哈哈', price: '123', stock: 10}
      ]
    }
  },
  watch: {
  },
  mounted() {
    this.getData()
    this.getGoodsList()
  },
  methods: {
    /**
     * 获取数据
     */
    getData() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        limit: 1000,
        page: this.page,
        status: this.status
      };
      merchantCashback(params).then(res => {
        console.log(res)
        this.tableData = res.data
      })
    },
    /**
     * 获取商品数据
     */
    getGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        limit: 1000,
        page: this.page,
        status: this.status
      }
      getGoodsList(params).then(res => {
        if(res.status === 200) {
          this.goodsList = res.data;
        }else if(res.status === 204) {
          this.goodsList = [];
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 切换分页
     */
    changePage() {

    },
    /**
     * 提交
     */
    post() {
      this.dialogVisible = false
      this.putMerchantCashback()
    },
    /**
     * 更改选择商品
     */
    putMerchantCashback(){
      putMerchantCashback({goods_id: this.checked,key: this.$store.state.app.activeApp.saa_key}).then(() => {
        this.getData()
      })
    },
    /**
     * 删除
     */
    handleDelete(e,i){
      console.log(i)
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: i.id
      }
      this.$confirm('此操作将永久删除该活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delMerchantCashback(params).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.marketing-coupon-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.header{
  span{
    font-size: 14px;
    color: #99a1ac;
  }
}
.goodsimg{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-row-gap: 10px;
  grid-column-gap: 10px;
  .goodsimg-item{
    width: 100%;
    height: auto;
  }
}
.dialog-list{
  width: 400px;
  margin-left: 50px;
  height: 300px;
  overflow: hidden;
  overflow-y: auto;
  z-index: 0;
  div{
    width: 200px;
    height: 40px;
    margin: 5px 0;
  }
}
.dialog-button{
  text-align: center;
  margin-top: 20px;
}
.list{
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-flow: row wrap;
  .list-item{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border: 1px solid #bbbbbb;
    height: 35px;
    box-sizing: border-box;
    padding: 10px 8px 10px 12px;
    margin: 10px 15px;
    border-radius: 3px;
    .item-picurl{
      width: 40px;
      height: 40px;
    }
    .item-yuan{
      color: red;
      font-size: 14px;
      margin-right: 4px;
    }
    
  }
}
</style>